#pragma ident "$Id: PhaseBiasData.cpp 2897 2012-08-10 20:08:46Z shjzhang $"

/**
 * @file PhaseBiasData.cpp
 * Encapsulate satellite upd file data, including I/O
 */

//============================================================================
//
//  Shoujian Zhang, Wuhan University, 2012
//
//============================================================================

#include "PhaseBiasStream.hpp"
#include "PhaseBiasHeader.hpp"
#include "PhaseBiasData.hpp"
#include "StringUtils.hpp"
#include "TimeString.hpp"
#include "CivilTime.hpp"

using namespace gpstk::StringUtils;
using namespace std;

#define debug 0

namespace gpstk
{

   void PhaseBiasData::reallyPutRecord(FFStream& ffs) const
      noexcept(false)
   {
         // cast the stream to be an PhaseBiasStream
      PhaseBiasStream& strm = dynamic_cast<PhaseBiasStream&>(ffs);

      int i;
      string line;

      line = recType;
      line += string(1,' ');

      if(recType == string("AR"))
      {
         line += rightJustify(site,4);
      }
      else if(recType == string("AS"))
      {
         line += string(1,sat.systemChar());
         line += rightJustify(asString(sat.id),2);
         if(line[4] == ' ') line[4] = '0';
         line += string(1,' ');
      }
      else
      {
         FFStreamError e("Unknown data type: " + recType);
         GPSTK_THROW(e);
      }

      line += string(1,' ');

      line += printTime(time,"%4Y %02m %02d %02H %02M %9.6f");


      line += rightJustify(asString(numData),3);

      line += rightJustify(StringUtils::asString(type), 6);
      line += rightJustify(StringUtils::asString(bias,3), 9);

      strm << line << endl;
      strm.lineNumber++;

   }  // end reallyPutRecord()

   void PhaseBiasData::reallyGetRecord(FFStream& ffs)
      noexcept(false)
   {
         // cast the stream to be an PhaseBiasStream
      PhaseBiasStream& strm = dynamic_cast<PhaseBiasStream&>(ffs);

      clear();

      string line;
      strm.formattedGetLine(line,true);      // true means 'expect possible EOF'
      stripTrailing(line);
      if(line.length() < 48)
      {
         FFStreamError e("Short line : " + line);
         GPSTK_THROW(e);
      }

      if(debug)
      cout << "Data Line: /" << line << "/" << endl;

      vector<string> dataVec = split(line, " ");

      // rec type
      recType = dataVec[0];

      if(recType == string("AS"))
      {
         // satID
         string satStr = dataVec[1];

         int prn(asInt(satStr.substr(1,2)));
         if(satStr[0] == 'G')
         {
            sat = SatID(prn,SatelliteSystem::GPS);
         }
         else if(satStr[0] == 'R')
         {
            sat = SatID(prn,SatelliteSystem::GLONASS);
         }
         else if(satStr[0] == 'E')
         {
            sat = SatID(prn,SatelliteSystem::Galileo);
         }
         else if(satStr[0] == 'B')
         {
            sat = SatID(prn,SatelliteSystem::BDS);
         }

         CivilTime ct( asInt(dataVec[2]),
                       asInt(dataVec[3]),
                       asInt(dataVec[4]),
                       asInt(dataVec[5]),
                       asInt(dataVec[6]),
                       asDouble(dataVec[7]),
                       TimeSystem::Any);
         time = ct;
         
         type = TypeID(strip(dataVec[9]));

         bias = asDouble(dataVec[10]);
      }

   }   // end reallyGetRecord()

   void PhaseBiasData::dump(ostream& s) const throw()
   {
      // dump record type, sat id / site, current epoch, and data
      if(recType == string("AS"))
      {
          s << " " << sat.toString() << " ";
          s << " " << printTime(time,"%Y/%02m/%02d %2H:%02M:%06.3f = %F/%10.3g %P");
          s << " " << type;
          s << fixed << setprecision(4) << " " << setw(8) << bias;
          s << "End of PhaseBiasData dump";
          s << endl;
      }

   }  // end dump()

} // namespace

//------------------------------------------------------------------------------------
